package com.starblink.brand.ui

import android.view.View
import android.widget.FrameLayout
import androidx.core.view.postDelayed
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.ext.bindFragment
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.margin
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.extension.px2dp
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.widget.LoadingDialogManager
import com.starblink.basic.route.RoutePage
import com.starblink.basic.style.R
import com.starblink.basic.util.ActivityStackManage
import com.starblink.basic.util.CommUtils
import com.starblink.brand.databinding.ActivityBrandAllBinding
import com.starblink.brand.model.BrandAllVM

@Route(path = RoutePage.Brand.BRAND_ALL)
class BrandAllActivity : BaseTVMActivity<ActivityBrandAllBinding, BrandAllVM>() {

    private var pageChangeSelected=0
    private val discountedFragment by lazy {DiscountedFragment.obtainFragment()}
    private val allFragment by lazy {AllFragment.obtainFragment()}

    override fun initViewBinding(): ActivityBrandAllBinding {
        return ActivityBrandAllBinding.inflate(layoutInflater)
    }

    override fun initData() {
        super.initData()
    }

    override fun initView() {
        super.initView()
        pageExposure(SpmPageDef.BrandAllPage)

        val fragmentList: List<Fragment> = listOf(
            discountedFragment,
            allFragment
        )
        viewBinding.vpTab.bindFragment(
            supportFragmentManager,
            fragmentList,
            pageChangeSelected = {
                pageChangeSelected=it
            })
        //界面禁止回收
        viewBinding.vpTab.offscreenPageLimit = fragmentList.size

        viewBinding.tvTab1.setOnClickListener {
            changeTab(0)
        }
        viewBinding.tvTab2.setOnClickListener {
            if(!allFragment.hasBrandData()){
                LoadingDialogManager.get().showLoading(this)
                CommUtils.getHandler()
                    .postDelayed(
                        {
                            LoadingDialogManager.get().dismissLoading()
                        },
                        3000
                    )//3秒后隐藏进度条
            }
            changeTab(1)
        }

        viewBinding.llSearch.trackData(page = SpmPageDef.BrandAllPage,
            element = SpmElementDef.ELE_33002,
            onlyHit = true)
        viewBinding.llSearch.setOnClickListener {
            navigationTo(RoutePage.Brand.BRAND_ALL_SEARCH)
        }
    }

    override fun initObservable() {
        super.initObservable()
    }

    //tab 切换
    private fun changeTab(position:Int){
        viewBinding.vpTab.setCurrentItem(position, false)
        viewBinding.tvTab1.setTextColor(resources.getColor(R.color.gu_white_06))
        viewBinding.tvTab1.setBackgroundResource(R.color.transparent)
        viewBinding.tvTab2.setTextColor(resources.getColor(R.color.gu_white_06))
        viewBinding.tvTab2.setBackgroundResource(R.color.transparent)
        when(position){
            0->{
                viewBinding.tvTab1.setTextColor(resources.getColor(R.color.gu_text_primary))
                viewBinding.tvTab1.setBackgroundResource(com.starblink.brand.R.drawable.bg_rect_white_corner)
                viewBinding.llRoundTab.margin(12.dp2px(),12.dp2px(),12.dp2px(),4.dp2px())
            }
            1->{
                viewBinding.tvTab2.setTextColor(resources.getColor(R.color.gu_text_primary))
                viewBinding.tvTab2.setBackgroundResource(com.starblink.brand.R.drawable.bg_rect_white_corner)
                viewBinding.llRoundTab.margin(12.dp2px(),12.dp2px(),12.dp2px(),16.dp2px())
            }
        }
    }
}